package com.cheapflix.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.cheapflix.beans.User;
import com.cheapflix.dao.UserDAO;

/**
 * Controller class in charge of authentication/logout.
 * @author ferru001
 */
@Controller
public class LoginController {

    private UserDAO userDao;

    @Autowired
    public void init(UserDAO userDao) {
        this.userDao = userDao;
    }
    @RequestMapping("/")
    public ModelAndView alreadyLogged(HttpSession session){
            User user = (User)session.getAttribute("me");
            if(user!=null){
                return new ModelAndView("redirect:/home/");
            }else{
                 return new ModelAndView("login");
            }
    }
    
    @RequestMapping("/login")
    public ModelAndView login(@RequestParam("username") String username,
            @RequestParam("password") String password,
            HttpSession session) {
        //Login logic goes here
        System.out.println("Logging in as " + username + "/" + password);
        User user = userDao.getUser(username, password);
        
        if (user != null) {
            //If correct username and pass go to home
            session.setAttribute("me", user);
            System.out.println("Valid login");
            ModelAndView mav = new ModelAndView();
            mav.setView(new RedirectView("/cheapflix/home/"));
            return mav;
        } else {
            System.out.println("Invalid login");
            return null;
        }
    }

    @RequestMapping("/register")
    public ModelAndView register(@RequestParam("username") String username,
            @RequestParam("password1") String password1,
            @RequestParam("password2") String password2,
            @RequestParam("nameFirst") String nameFirst,
            @RequestParam("nameLast") String nameLast,
            @RequestParam("email") String email,
            HttpSession session) {
        //Register logic goes here
        System.out.println("Registering...?");

        User user = new User();

        user.setUsername(username);
        user.setNameFirst(nameFirst);
        user.setNameLast(nameLast);
        user.setEmail(email);
        user.setPassword(password1);

        userDao.addUser(user);

        ModelAndView mav = new ModelAndView();
        mav.setView(new RedirectView("/cheapflix/"));
        mav.addObject("loginSuccesssMessage", "Registration Successfull! You may not login");
        return mav;
    }

    @RequestMapping("/logout")
    public ModelAndView register(
            HttpSession session) {
        session.removeAttribute("me");

        ModelAndView mav = new ModelAndView();
        mav.setView(new RedirectView("/cheapflix/"));
        return mav;
    }
}
